package com.example.deliveryapp;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class ViewOrderPageActivity extends AppCompatActivity {
    private EditText editText_chickenwings_vieworder, editText_yamfries_vieworder,editText_cutlery_vieworder;
    private Button minus_chikenwings_button, add_chikenwings_button, minus_yamfries_vieworder, add_yamfries_vieworder;
    private Button minus_cutlery_vieworder,add_cutlery_vieworder;
    private TextView price_chickenwings_vieworder, price_yamfries_vieworder,totalprice_vieworder;
    private double unitprice_chickenwings =17.00,unitprice_yamfries =9.35,totalprice = 0.00;
    private int totalQuantity = 0;
    private EditText editTextName_vieworder,editTextPhone_vieworder,editTextProvince_vieworder,editTextCity_vieworder,editTextArea_vieworder,editTextAdress_vieworder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        // 设置内容视图为您的布局XML
        setContentView(R.layout.view_order_page);
        //返回上一个Activity
        Button back_customize_order = findViewById(R.id.back_view_order);
        back_customize_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                finish();//返回上一个Activity
            }
        });
        //检查订单，准备付款
        Button checkout_view_order = findViewById(R.id.checkout_view_order);
        checkout_view_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ViewOrderPageActivity.this, CheckOutPageActivity.class);
                startActivity(intent);
            }
        });
        editText_yamfries_vieworder = findViewById(R.id.editText_yamfries_vieworder);
        add_yamfries_vieworder = findViewById(R.id.add_yamfries_vieworder);
        minus_yamfries_vieworder = findViewById(R.id.minus_yamfries_vieworder);
        price_yamfries_vieworder = findViewById(R.id.price_yamfries_vieworder);
        editText_chickenwings_vieworder =findViewById(R.id.editText_chickenwings_vieworder);
        add_chikenwings_button =findViewById(R.id.add_chickenwings_vieworder);
        minus_chikenwings_button =findViewById(R.id.minus_chickenwings_vieworder);
        price_chickenwings_vieworder =findViewById(R.id.price_chickenwings_vieworder);
        add_cutlery_vieworder = findViewById(R.id.add_cutlery_vieworder);
        minus_cutlery_vieworder =findViewById(R.id.minus_cutlery_vieworder);
        editText_cutlery_vieworder = findViewById(R.id.editText_cutlery_vieworder);
        totalprice_vieworder = findViewById(R.id.totalprice_vieworder);



        setupButtons1();
        setupEditText1();
        setupButtons2();
        setupEditText2();
        setupButtons3();
        setupEditText3();
        updateTotalPriceDisplay();
    }
    private  void setupButtons3(){
        add_cutlery_vieworder.setOnClickListener(view ->updateQuantity3(1));
        minus_cutlery_vieworder.setOnClickListener(view ->updateQuantity3(-1));
    }
    private void setupEditText3(){
        editText_cutlery_vieworder.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            updateprice3();
            }
        });
    }
    private void updateprice3(){
        int quantity3 =getSafeInt3(editText_cutlery_vieworder);
        double totalprice1 = unitprice_yamfries * quantity3;
        updateTotalPrice();
    }
    private void  updateQuantity3(int change3){
        int currentValue3 = getSafeInt3(editText_cutlery_vieworder);
        int newQuantity3 = currentValue3 + change3;
        if(newQuantity3 >= 0 && newQuantity3 <= 99){
            editText_cutlery_vieworder.setText(String.valueOf(newQuantity3));
            updateprice3();
            updateTotalPrice();
        }
    }
    private int getSafeInt3(EditText editText){
        try {
            return Integer.parseInt(editText.getText().toString());
        }catch (NumberFormatException e){
            return 0;
        }
    }
    private void setupButtons1(){
        add_yamfries_vieworder.setOnClickListener(view ->updateQuantity1(1));
        minus_yamfries_vieworder.setOnClickListener(view ->updateQuantity1(-1));
    }
    private void setupEditText1(){
        editText_yamfries_vieworder.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                updateprice1();
            }
        });
    }
    private void updateprice1(){
        int quantity1 =getSafeInt1(editText_yamfries_vieworder);
        double totalprice1 = unitprice_yamfries * quantity1;
        String formattedPrice1 = String.format(Locale.getDefault(),"Price:%.2f",totalprice1);
        price_yamfries_vieworder.setText(formattedPrice1);
        updateTotalPrice();
    }
    private void updateQuantity1(int change1){
        int currentValue1 = getSafeInt1(editText_yamfries_vieworder);
        int newQuantity1 = currentValue1 + change1;
        if (newQuantity1 >=0 && newQuantity1 <= 99){
            editText_yamfries_vieworder.setText(String.valueOf(newQuantity1));
            updateprice1();
            updateTotalPrice();
        }
    }
    private int  getSafeInt1(EditText editText){
        try {
            return Integer.parseInt(editText.getText().toString());
        }catch (NumberFormatException e){
            return 0;
        }

    }
    private void setupButtons2(){
        add_chikenwings_button.setOnClickListener(view ->updateQuantity2(1));
        minus_chikenwings_button.setOnClickListener(view ->updateQuantity2(-1));
    }
    private void  setupEditText2(){
        editText_chickenwings_vieworder.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                updateprice2();
            }
        });
    }
    private void updateprice2(){
        int quantity2 = getSafeInt2(editText_chickenwings_vieworder);
        double totalprice2 = unitprice_chickenwings * quantity2;
        String formattedPrice2 = String.format(Locale.getDefault(),"Price:%.2f",totalprice2);
        price_chickenwings_vieworder.setText(formattedPrice2);
    }
    private void updateQuantity2(int change2){
        int currentValue2 = getSafeInt2(editText_chickenwings_vieworder);
        int newQuantity2 = currentValue2 + change2;
        if(newQuantity2 >=0 && newQuantity2 <=99){
            editText_chickenwings_vieworder.setText(String.valueOf(newQuantity2));
            updateprice2();
            updateTotalPrice();
        }
    }
    private int getSafeInt2(EditText editText){
        try {
            return  Integer.parseInt(editText.getText().toString());
        }catch (NumberFormatException e){
            return 0;
        }
    }
    private void updateTotalPrice(){
        totalprice = (getSafeInt1(editText_yamfries_vieworder)*unitprice_yamfries) +
                (getSafeInt2(editText_chickenwings_vieworder)*unitprice_chickenwings) ;
        updateTotalPriceDisplay();
    }
    private void  updateTotalPriceDisplay(){
        totalprice_vieworder.setText("$"+String.format(Locale.getDefault(),"%.2f",totalprice));
    }
    private int getSafeInt(EditText editText){
        try {
            return Integer.parseInt(editText.getText().toString());
        }catch (NumberFormatException e){
            return 0;
        }
    }
}
